const fg = require('fast-glob')

/**
 * Returns an array of objects containing the name and version of all non-private packages
 * in the `packages` directory of the current working directory.
 *
 * @returns {array} An array of objects, where each object contains the `name` and `version`
 *                   properties of a package.
 */
function getAllWorkspacePackageInfo() {
  // actually，need to use root/package.json/workspace to replace,but we only need to publish packages where in root/packages
  const packagePaths = fg.sync(`${process.cwd()}/packages/*/package.json`, { onlyFiles: true })
  return (
    packagePaths
      .map((path) => require(path))
      // compatible private: 'false'
      .filter((item) => item.private === 'false' || !item.private)
      .map(({ name, version }) => ({ name, version }))
  )
}

function getPackageInfoWithPath() {
  const packagePaths = fg.sync(`${process.cwd()}/packages/*/package.json`, { onlyFiles: true })
  return packagePaths.map((path) => {
    const { name, version } = require(path)
    return {
      path: path,
      name,
      version,
    }
  })
}

module.exports = {
  getAllWorkspacePackageInfo,
  getPackageInfoWithPath,
}
